<?php
namespace app\index\controller;
use think\Db;
use think\Controller;


//header("Access-Control-Allow-Origin:http://152.136.128.96"); // 允许指定域访问跨域资源
//header("Access-Control-Allow-Origin:http://sparkdesignbox.cn"); // 允许指定域访问跨域资源
header("Access-Control-Allow-Credentials:true");
header("Set-Cookie: hidden=value; httpOnly");//预防XSS攻击

class Quescontroller extends Controller
{

    public function hasAnsweredQues($userId, $quesId){
        $flag = Db::table('sheji.question_bank')
            ->field('special_flag')
            ->where(['id' => $quesId])
            ->find();

        if($flag['special_flag'] == 'targetUser'){
            $list2 = Db::table('sheji.ques_records')
                ->field('id')
                ->where(['author_id' => $userId, 'ques_id' => $quesId])
                ->find();

            if(sizeof($list2) == 0){
                $result = ['has' => false];
                return json_encode($result);
            }
        }else {

            $list = Db::table('sheji.ques_prepared_records')
                ->field('*')
                ->where(['ques_id' => $quesId, 'readonly' => 'N'])
                ->select();

            foreach ($list as $item) {
                $list2 = Db::table('sheji.ques_records')
                    ->field('id')
                    ->where(['author_id' => $userId, 'prepared_id' => $item['id']])
                    ->find();
                if (sizeof($list2) == 0) {
                    $result = ['has' => false];
                    return json_encode($result);
                }
            }
        }

        $result = ['has' => true];
        return json_encode($result);
    }

    public function getQuesAnswer($quesId){
        $list = Db::table('sheji.question_bank')
            ->field('answers')
            ->where(['id' => $quesId])
            ->find();

        return $list['answers'];
    }

    public function getQuesSelfAssessment($userId, $quesId){
        $list = Db::table('sheji.ques_self_assessment')
            ->field('*')
            ->where(['user_id' => $userId, 'ques_id' => $quesId])
            ->find();

        Db::table('sheji.user_history')
            ->data(['user_id' => $userId, 'bh_type' => 'lookup_ques_answer', 'bh_time' => date('Y-m-d H:i:s', time()), 'tool_id' => $quesId])
            ->insert();


        return json_encode($list);
    }

    public function updateQuesSelfAssessment($userId, $quesId, $ratings, $comments){
        $list = Db::table('sheji.ques_self_assessment')
            ->field('*')
            ->where(['user_id' => $userId, 'ques_id' => $quesId])
            ->find();

        if(sizeof($list) > 0){
            Db::table('sheji.ques_self_assessment')
                ->where(['user_id' => $userId, 'ques_id' => $quesId])
                ->update(['ratings' => $ratings, 'comments' => $comments]);
        }else{
            Db::table('sheji.ques_self_assessment')
                ->data(['user_id' => $userId, 'ques_id' => $quesId, 'ratings' => $ratings, 'comments' => $comments])
                ->insert();
        }

        Db::table('sheji.user_history')
            ->data(['user_id' => $userId, 'bh_type' => 'self_assessment_ques', 'bh_time' => date('Y-m-d H:i:s', time()), 'tool_id' => $quesId])
            ->insert();
        //return $list;
    }

    public function updateQuesFlagRecords($preparedId, $userId, $isFlagWanted){
        $list = Db::table('sheji.ques_flag_records')
            ->where(['user_id' => $userId, 'prepared_id' => $preparedId])
            ->find();

        if(sizeof($list) > 0){
            Db::table('sheji.ques_flag_records')
                ->where(['user_id' => $userId, 'prepared_id' => $preparedId])
                ->update(['importance_flag' => $isFlagWanted]);
        }else{
            Db::table('sheji.ques_flag_records')
                ->data(['user_id' => $userId, 'prepared_id' => $preparedId, 'importance_flag' => $isFlagWanted])
                ->insert();
        }

        $bh_type = 'importance_flag_ques_'.$isFlagWanted;
        Db::table('sheji.user_history')
            ->data(['user_id' => $userId, 'bh_type' => $bh_type, 'bh_time' => date('Y-m-d H:i:s', time()), 'record_id' => $preparedId])
            ->insert();
    }


}